//
//  HRStatusListViewModel.swift
//  microBlog
//
//  Created by HR_W on 16/5/25.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

class HRStatusListViewModel: NSObject {

    lazy var statusViewModelArray: [HRStatusViewModel] = [HRStatusViewModel]()
    
    
    func loadData(isPulling: Bool,finished: (isSuccess: Bool) -> ()) {
        var since_id: Int64 = 0
        var max_id: Int64 = 0
        
        if isPulling{
            max_id = statusViewModelArray.last?.status?.id ?? 0
        }else{
            since_id = statusViewModelArray.first?.status?.id ?? 0
        }
        HMNetworkTools.sharedTools.loadHomePageData(since_id, max_id: max_id) { (responseObject, error) in

            if error != nil {
                
               // SVProgressHUD.showErrorWithStatus(AppErrorTip)
                finished(isSuccess: false)
                return
            }
            
            //print(responseObject!)
            
            guard let dict = responseObject as? [String: AnyObject] else{
                 finished(isSuccess: false)
                return
            }
            
            guard let array = dict["statuses"] as? [[String: AnyObject]] else {
                 finished(isSuccess: false)
                return
            }
            // print(array)
            
            var tempArray = [HRStatusViewModel]()
            
            for item in array {
                
                let s = HRStatus(dict: item)
                
                let viewModel = HRStatusViewModel(status: s)
                
                tempArray.append(viewModel)
            }
            if isPulling{
            self.statusViewModelArray = self.statusViewModelArray + tempArray
             
            }else{
            
             self.statusViewModelArray = tempArray + self.statusViewModelArray
            }
            
            
            
           finished(isSuccess: true)
        }
    }

    
}
